//   全局方法

export default class DateUtil {
  constructor () {
  }

  static toDate (date) {
    let d = date
    if (d) {
      if (typeof d === 'string' || typeof d === 'number') {
        d = new Date(d)
      }
    } else {
      d = new Date()
    }
    return d
  }

  static getDayStart (date) {
    date = this.toDate(date)
    return new Date(date.toLocaleDateString()).getTime()
  }

  static getDayEnd (date) {
    date = this.toDate(date)
    return new Date(date.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1000
  }

  static getMonthStart (date) {
    date = this.toDate(date)
    let y = date.getFullYear(), m = date.getMonth()
    return new Date(y, m, 1).getTime()
  }

  static getMonthEnd (date) {
    date = this.toDate(date)
    let y = date.getFullYear(), m = date.getMonth()
    return new Date(y, m + 1, 0).getTime() + 24 * 60 * 60 * 1000 - 1000
  }

  static getYearStart (date) {
    date = this.toDate(date)
    let y = date.getFullYear()
    return new Date(y, 1, 1).getTime()
  }

  static getYearEnd (date) {
    date = this.toDate(date)
    let y = date.getFullYear()
    return new Date(y + 1, 0, 0).getTime() + 24 * 60 * 60 * 1000 - 1000
  }

  /**
   * 时间戳日期时间格式
   *
   * @param val //时间戳 必填
   * 返回值 格式化后的时间字符串
   *
   */
  static timeTodate (val) {
    let date = new Date(val)
    let seperator1 = '-'
    let seperator2 = ':'
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let days = date.getDate()
    let hours = date.getHours()
    let minutes = date.getMinutes()
    let seconds = date.getSeconds()
    if (month >= 0 && month <= 9) {
      // @ts-ignore
      month = '0' + month
    }
    if (days >= 0 && days <= 9) {
      // @ts-ignore
      days = '0' + days
    }
    if (hours >= 0 && hours <= 9) {
      // @ts-ignore
      hours = '0' + hours
    }
    if (minutes >= 0 && minutes <= 9) {
      // @ts-ignore
      minutes = '0' + minutes
    }
    if (seconds >= 0 && seconds <= 9) {
      // @ts-ignore
      seconds = '0' + seconds
    }
    let time = year + seperator1 + month + seperator1 + days +
      ' ' + hours + seperator2 + minutes +
      seperator2 + seconds
    return time
  }

  /**
   * 时间戳日期时间格式
   *
   * @param time // 时间戳 必填
   * @param formatType //格式化方式 Y年(YY年份，YYYY全写),M月,D日,H时24制，h为12制，m分s秒,w周（ww全写），默认YYYY-M-D H:m:s ww
   * 返回值 格式化后的时间字符串
   *
   */
  static dateFormat (time, formatType) {
    if (!time) {
      return ''
    }
    formatType = formatType || 'YYYY-M-D H:m:s ww'
    let nTime = Number(time)
    let date = new Date(nTime)
    let o = {
      'M': date.getMonth() + 1, //    月份
      'D': date.getDate(), //    日
      'h': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, //    小时
      'H': date.getHours(), //    小时
      'm': date.getMinutes(), //    分
      's': date.getSeconds(), //    秒
      'q': Math.floor((date.getMonth() + 3) / 3), //    季度
      'S': date.getMilliseconds() //    毫秒
    }
    let year = formatType.match(/(y+)/i)
    let subWeek = formatType.match(/(w+)/i)
    let week = {
      '0': '日',
      '1': '一',
      '2': '二',
      '3': '三',
      '4': '四',
      '5': '五',
      '6': '六'
    }
    if (year && year[0]) {
      formatType = formatType.replace(year[0], (date.getFullYear() + '').substr(4 - year[0].length))
    }
    if (subWeek && subWeek[0]) {
      formatType = formatType.replace(subWeek[0], ((subWeek[0].length > 1) ? (subWeek[0].length > 2 ? '星期' : '周') : '') + week[date.getDay() + ''])
    }
    for (let k in o) {
      if (new RegExp('(' + k + ')').test(formatType)) {
        formatType = formatType.replace(k, (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return formatType
  }

  static formatDuring (mss) {
    // @ts-ignore
    var days = parseInt(mss / (1000 * 60 * 60 * 24))
    // @ts-ignore
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
    // @ts-ignore
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60))
    // @ts-ignore
    var seconds = parseInt((mss % (1000 * 60)) / 1000)
    var str = ''
    if (days > 0) {
      str = days + ' 天 '
    } else if (hours > 0) {
      str = hours + ' 小时 '
    } else if (minutes > 0) {
      str = minutes + ' 分钟 '
    } else if (seconds > 0) {
      str = seconds + ' 秒 '
    }
    return str
  }

  /**
   * 时间戳转时分秒
   *
   * @param val //时间戳 必填
   * 返回值 格式化后的时间字符串
   *
   */
  static formatDuringDHM (mss) {
    // @ts-ignore
    var days = parseInt(mss / (1000 * 60 * 60 * 24))
    // @ts-ignore
    var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
    // @ts-ignore
    var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60))
    // @ts-ignore
    var seconds = parseInt((mss % (1000 * 60)) / 1000)
    var str = ''
    if (days > 0) {
      str += days + ' 天 '
    }
    if (hours > 0) {
      str += hours + ' 小时 '
    }
    if (minutes > 0) {
      str += minutes + ' 分钟 '
    }
    if (str) {
      return str
    } else {
      return '<1分钟'
    }
  }

  static secondToHHMM (second) {
    // @ts-ignore
    let hours = parseInt(second / 60 / 60)
    if (hours < 10) {
      // @ts-ignore
      hours = '0' + hours
    }
    // @ts-ignore
    let minutes = parseInt(second / 60 % 60)
    if (minutes < 10) {
      // @ts-ignore
      minutes = '0' + minutes
    }
    return hours + ':' + minutes
  }

}
